﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SYH.Domain;
using SYH.Domain.Common;
using SYH.Domain.ViewModels;
using SYH.IBLL;
using SYH.Web.Models;
using SYH.Domain.ViewModels.Common;

namespace SYH.Web.Controllers.Common
{
    public class AccAttaFileController : BaseController
    {
        // GET: AccAttaFile
        private readonly IEntityRepository<BusinessFile> _FileService;
        private readonly IEntityRepository<AccAssociation> _AttaService;
        private readonly string tempPath = ConfigurationManager.AppSettings["DefaultTempUploadUrl"];

        public AccAttaFileController(IEntityRepository<BusinessFile> FileService,
            IEntityRepository<AccAssociation> AttaService)
        {
            _FileService = FileService;
            _AttaService = AttaService;
        }

        /// <summary>
        /// 附件查看
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Show(Guid id)
        {
            var bo = _AttaService.GetBo(id);
            if (bo == null)
            {
                return Error("找不到附件！");
            }
            var bo1 = _FileService.GetBo(bo.AnnexID);
            if (bo == null)
            {
                return Error("找不到附件！");
            }
            var view = new ShowAttaVM
            {
                ID = bo.ID,
                Body = bo.Body,
                Ext = bo1.UploadFileSuffix,
                Name = bo1.Name,
                FileName = bo1.OriginalFileName,
                FilePath = tempPath.Replace(@"\", "/")
            };
            return JsonData(view);
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult RemoveAtta(string id, string fid)
        {
            
            if (!string.IsNullOrEmpty(id)&&id!=Guid.Empty.ToString())
            {
                var aid = Guid.Parse(id);
                PublicCs.Delete(aid);
            }
            else if (!string.IsNullOrEmpty(fid))
            {
                var f = Guid.Parse(fid);
                PublicCs.DelFile(f);
            }
            return Success();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowFile(Guid id)
        {
            var bo = _FileService.GetBo(id);
            if (bo == null)
            {
                return Error("找不到附件！");
            }
            var view = new ShowAttaVM
            {
                ID = bo.ID,
                Ext = bo.UploadFileSuffix,
                Name = bo.Name,
                FileName = bo.OriginalFileName,
                FilePath = tempPath.Replace(@"\", "/")
            };
            return JsonData(view);
        }
    }
}